package com.medical.data.utils;

import org.apache.commons.compress.utils.Lists;
import org.dozer.DozerBeanMapper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class DozerUtils {

    static DozerBeanMapper dozerBeanMapper = new DozerBeanMapper();

    //转化list
    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass){
        List destinationList = Lists.newArrayList();
        for (Iterator i$ = sourceList.iterator(); i$.hasNext();){
            Object sourceObject = i$.next();
            Object destinationObject = dozerBeanMapper.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }
    //将集合转化成stream流
    public static InputStream convert(Map<String,Object> map) throws IOException {
        ByteArrayOutputStream outputstream = new ByteArrayOutputStream( ) ;

        for (Iterator<Map.Entry<String, Object>> i$ = map.entrySet().stream().iterator(); i$.hasNext();){
            Object sourceObject = i$.next();
            System.out.println(sourceObject);
        }
        for (Map.Entry<String,Object> entry : map. entrySet()) {
            String key = entry.getKey( );
            Object value = entry .getValue();
            String line = key + "=" + value + "\n";
            outputstream.write(line.getBytes());
        }
        outputstream.flush( );
        byte[] bytes = outputstream.toByteArray( );
        outputstream.close();
        return new ByteArrayInputStream( bytes);
    }

    public static void main(String[] args) throws IOException {
        Map<String,Object> map = new HashMap<>();
        map.put("1111",111);
        List<String> list = new ArrayList<>();
        list.add("22222");
        map.put("2222",list);
        convert(map);
    }
}
